package com.action.system;
/**
 * 版本综合信息:  $Id: Test.java 35 2014-06-19 09:06:40Z karl.li $
 * 最后提交人： $Author: karl.li $
 * 最后提交日期: $Date: 2014-06-19 17:06:40 +0800   $
 * SVN版本号： $Rev: 35 $
 * 源文件URL: $URL: https://teleframe.f3322.org/svn/iparking/branches/iparking/src/java/com/action/system/Test.java $
 * 
 */

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import net.sf.json.JSONArray;

import com.base.BaseAction;
import com.bean.RulerInfo;
import com.bean.RulerMethod;
import com.service.RulerService;
import com.util.GUtil;

public class RulerAction extends BaseAction {
	private static final long serialVersionUID = 1L;

	List<RulerInfo> list;
	RulerInfo rulerInfo;
	List<RulerMethod> methods;
	Integer pid;
	String pname;
	

	@Override
	public String handle() throws Exception {
		
		if(method==null){
			return query();
		}
		return GUtil.NONE;
		
	}
	
	public String add() throws Exception {
		this.setHeaderTitle("添加菜单");
		list = rulerService.query(null);
		if(rulerInfo==null || (rulerInfo.getRulerName()==null && rulerInfo.getManager()!=null && rulerInfo.getLevel()==2)){
			return GUtil.ADD;
		}
		
		boolean bOk = true;
		if(rulerInfo.getRulerName()==null || rulerInfo.getRulerName().trim().isEmpty()){
			this.addFieldError("rulerInfo.rulerName", "菜单名称不能为空");
			bOk = false;
		}
		
		if(bOk==false)
			return GUtil.ADD;
		
		pid = rulerInfo.getManager();
		if(pid==null || pid<1)
			rulerInfo.setLevel(1);//默认添加的是一级菜单
		else
			rulerInfo.setLevel(2);
//		System.out.println("rulerparent="+rulerInfo.getRulerid());
		rulerInfo.setMethods(methods);
		String result = rulerService.add(rulerInfo);
		this.setPid(rulerInfo.getManager());
		this.setPname(rulerInfo.getManagerName());
		if(result.equals(GUtil.SUCCESS)){
			msg = "添加成功";
			return  GUtil.ADD;
		}else {
			msg = "添加失败";
			return GUtil.ADD;
		}
	}

	public String delete() throws Exception {
		String result = rulerService.delete(id);
		if(result.equals(GUtil.SUCCESS)){
			msg = "删除成功";
			return  query();
		}else {
			msg = "删除失败";
			return GUtil.LIST;
		}
	}
 
	public String query() throws Exception {
		this.setLogmsg("查询菜单");
		list = rulerService.query(rulerInfo);
		return GUtil.LIST;
	}
	
	public String update() throws Exception {
		this.setHeaderTitle("修改菜单");
		//beforeUpdate
		list = rulerService.list(null);
		if(rulerInfo==null){
			if(id<1){
				msg = "请求参数错误";
				return GUtil.ERROR;
			}
			rulerInfo = rulerService.get(id);
			session.put("rulerInfo", rulerInfo);
			return GUtil.UPDATE;
		}
		RulerInfo rf = (RulerInfo) session.get("rulerInfo");
		if(rf==null){
			msg = "修改失败";
			return GUtil.UPDATE;
		}
		id = rf.getRulerid();
//		System.out.println("methods="+methods);
		rulerInfo.setMethods(methods);
		String result = rulerService.update(id, rulerInfo);
		rulerInfo=null;
		if(result.equals(GUtil.SUCCESS)){
			msg = GUtil.UpdateSuccess;
			return query();
		}
		else{
			msg = "修改失败";
			return GUtil.UPDATE;
		}
	}
	
	@Deprecated
	public String listTree(){
		list = rulerService.listTreeRuler();
		String jsonstr="";
		System.out.println("listTree PrintWriter ");
		if(list!=null)
		{
			JSONArray json = JSONArray.fromObject(list);
			jsonstr = json.toString();
		}
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=utf-8"); 
		response.setHeader("pragma", "no-cache");  
		response.setHeader("cache-control", "no-cache");
		try {
			PrintWriter out = response.getWriter();
			out.write(jsonstr);
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println("listTree return ");
		return GUtil.NONE;
	}

	public List<RulerInfo> getList() {
		return list;
	}

	public void setList(List<RulerInfo> list) {
		this.list = list;
	}

	public RulerInfo getRulerInfo() {
		return rulerInfo;
	}

	public void setRulerInfo(RulerInfo rulerInfo) {
		this.rulerInfo = rulerInfo;
	}

	@Override
	public String view() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}


	public String getPname() {
		return pname;
	}


	public void setPname(String pname) {
		this.pname = pname;
	}

	public Integer getPid() {
		return pid;
	}

	public void setPid(Integer pid) {
		this.pid = pid;
	}

	@Override
	public boolean validate(Object obj) throws Exception {
		// TODO Auto-generated method stub
		return false;
	}

	/**
	 * @return the methods
	 */
	public List<RulerMethod> getMethods() {
		return methods;
	}

	/**
	 * @param methods the methods to set
	 */
	public void setMethods(List<RulerMethod> methods) {
		System.out.println("setMethods="+methods);
		this.methods = methods;
	}

}
